package com.mrkun.vueshop.mapper;

import com.mrkun.vueshop.DTO.MoneyDTO;
import com.mrkun.vueshop.model.Order;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author by yong
 * @date 2020/3/16
 */
@Mapper
public interface OrderMapper {

    @Select("select * from `order`")
    List<Order> findAll();

    @Select("select * from `order` where id = #{id}")
    Order findOne();

    @Insert("insert into `order` (create_time,fund_name,fund_id,money,end_time,state,user_id)" +
            "values (#{createTime},#{fundName},#{fundId},#{money},#{endTime},#{state},#{userId})")
    void addOrder(Order order);

    @Select("select * from `order` where state = 1")
    List<Order> findAllNotEnd();

    @Update("update `order` set `money` = #{money},`total_income` = #{totalIncome} where `id` = #{id}")
    void updateMoneyAndIncome(@Param("money")BigDecimal money,@Param("totalIncome")BigDecimal totalIncome,@Param("id")Integer id);

    @Select("select * from `order` where user_id = #{userId}")
    List<Order> findByUserId(@Param("userId")Integer userId);

    @Select("select sum(money) totalMoney,sum(total_income) totalIncome from `order` where user_id = #{userId}")
    MoneyDTO getTotalMoney(@Param("userId")Integer userId);

    @Update("update `order` set state = 2 where id = #{orderId}")
    void sellFund(@Param("orderId") Integer orderId);
}
